using System;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EntLibContrib.Data.SQLite.Tests
{
    [TestClass]
    
    public class SQLiteExecuteDatasetFixture
    {
        private const string queryString = "Select * from Region";
        private const string storedProc = "Ten Most Expensive Products";
        private Database db;

        [TestInitialize]
        public void TestInitialize()
        {
            DatabaseProviderFactory factory = new DatabaseProviderFactory(TestConfigurationSource.CreateConfigurationSource());
            db = factory.Create("SQLiteNorthwind");
        }

        [TestMethod, DeploymentItem("EntLibContrib.Data.SqLite.Tests.dll.config"), DeploymentItem("Northwind.db")]
        public void CanRetriveDataSetFromSqlString()
        {
            DataSet dataSet = db.ExecuteDataSet(CommandType.Text, queryString);
            Assert.AreEqual(1, dataSet.Tables.Count);
        }

        [TestMethod, DeploymentItem("EntLibContrib.Data.SqLite.Tests.dll.config"), DeploymentItem("Northwind.db")]
        [Ignore] //Stored procedures not currently supported for SQLite
        public void CanRetiveDataSetFromStoredProcedure()
        {
            DataSet dataSet = db.ExecuteDataSet(storedProc);
            Assert.AreEqual(1, dataSet.Tables.Count);
        }

        [TestMethod, DeploymentItem("EntLibContrib.Data.SqLite.Tests.dll.config"), DeploymentItem("Northwind.db")]
        public void CanRetriveDataSetFromSqlStringWithTransaction()
        {
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                using (DbTransaction trans = connection.BeginTransaction())
                {
                    DataSet dataSet = db.ExecuteDataSet(trans, CommandType.Text, queryString);
                    Assert.AreEqual(1, dataSet.Tables.Count);
                }
            }
        }

        [TestMethod, DeploymentItem("EntLibContrib.Data.SqLite.Tests.dll.config"), DeploymentItem("Northwind.db")]
        [Ignore] //Stored procedures not currently supported for SQLite
        public void CanRetiveDataSetFromStoredProcedureWithTransaction()
        {
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                using (DbTransaction trans = connection.BeginTransaction())
                {

                    DataSet dataSet = db.ExecuteDataSet(trans, storedProc);
                    Assert.AreEqual(1, dataSet.Tables.Count);
                }
            }
        }
    }
}
